<?php
/**
 * (jobs) Amazon产品变化时，更新关联基础数据
 */
namespace App\Jobs\Products;

use App\Models\Basics\AmazonAccount;
use App\Models\Basics\AmazonCountry;
use App\Models\Basics\ExchangeRate;
use App\Models\Basics\SysICountry;
use App\Models\Basics\SysPlat;
use App\Models\Basics\Transport;
use App\Models\MonthlyReport\Cost\LogisticsBasics;
use App\Models\Operation\ProfitSku;
use App\Models\Product\ProductAmazonSku;
use App\Models\Product\ProductAmazonSkuCCS;
use App\Models\Product\ProductPacking;
use App\Models\Product\ProductSku;
use App\Models\SsBasic\AreaSku;
use App\Models\SsBasic\CountrySku;
use App\Models\SsBasic\JpSku;
use App\Models\Stocking\StockingSetDate;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
class AmazonSkuRelate implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    protected $productAmazonSku;

    protected $userId;

    /**
     * AmazonSkuRelate constructor.
     * @param ProductAmazonSku $productAmazonSku
     * @param $userId
     */
    public function __construct(ProductAmazonSku $productAmazonSku,$userId)
    {
        $this->productAmazonSku = $productAmazonSku;
        $this->userId = $userId;
    }

    /**
     * Execute the job.
     * @return void
     */
    public function handle()
    {
        $this->exeFillData($this->productAmazonSku,$this->userId);
    }

    private function exeFillData($productAmazonSku,$userId){
        $this->_upTimes($productAmazonSku->seller_sku,$productAmazonSku->child_asin,$productAmazonSku->fnsku);
        $this->_other($productAmazonSku,$userId);
    }

    //标记泛欧状态
    private function _upTimes($sellerSku,$childAsin,$fnSku){
        $country = AmazonCountry::options(['fan_eu' => 1]);
        $countryIdsForFanEU = array_keys($country->toArray());
        $notInAccountId = AmazonAccount::$_notFanEu;
        ProductAmazonSku::upTimes($sellerSku,$childAsin,$fnSku,$countryIdsForFanEU,$notInAccountId);
    }

    private function _other(ProductAmazonSku $productAmazonSku,$userId){
        //更新产品表在售状态
        $sku = ProductSku::getModelById($productAmazonSku->psku_id);
        $sku->is_sales = 1;
        $sku->save();
        //更新销售
        $ccs = ProductAmazonSkuCCS::returnSellerId($productAmazonSku->country_id,$sku->classify_id,$userId);
        $productAmazonSku->seller_id = $ccs->seller_id ?? $userId;
        //$productAmazonSku->classify_id = $sku->classify_id ?? 0; //品类id
        $productAmazonSku->save();
//        ProductAmazonSkuCCS::upSellerId($productAmazonSku->country_id,$sku->classify_id,$ccs->seller_id);
        //添加毛利率表
        $currencyCode = AmazonCountry::getCurrencyCodeByCountryId($productAmazonSku->country_id);
        $rateOdds = ExchangeRate::getRateOddsToRMB($currencyCode,time());
        $areaId = SysICountry::getAreaIdByCountryId($productAmazonSku->country_id);
        $transport = Transport::getTransport($areaId);
        $shipment = LogisticsBasics::getLastLogistics($areaId,$productAmazonSku->psku_id);
        if($shipment){
            $avgFee = true;
            $airFee = $seaFee = $shipment->price;
        }else{
            $avgFee = false;
            $airFee = $transport->air_fee ?? 0;
            $seaFee = $transport->sea_fee ?? 0;
        }
        $tplRMB = ProductSku::getPuPriceNoTax($sku);
        $shipWeight = ProductPacking::getShipWeight($productAmazonSku->psku_id);
        $vat = AmazonCountry::getVatByCountryId($productAmazonSku->country_id);
        ProfitSku::addProfitSku($productAmazonSku,$rateOdds,$tplRMB,$shipWeight,$airFee,$seaFee,$vat,$avgFee);

        //添加备货，提货基础数据(备货站点sku)
        $pCycle = ProductSku::getPuCycle($sku);
        AreaSku::addAreaSku($areaId,$productAmazonSku->psku_id,$transport->air_day ?? 0,$transport->sea_day ?? 0,$pCycle,$tplRMB);

        //添加(EU国家)备货，提货基础数据(EU国家备货sku)
        if ($areaId == 4) {
            CountrySku::addCountrySku($productAmazonSku->country_id,$productAmazonSku->psku_id,$transport->air_day ?? 0,$transport->sea_day ?? 0,$pCycle,$tplRMB);
        }
        //添加(JP平台)备货，提货基础数据(JP平台备货sku)
        if ($areaId == 6) {
            JpSku::addJpSku(SysPlat::PLAT_AMAZON,$productAmazonSku->psku_id,$transport->air_day ?? 0,$transport->sea_day ?? 0,$pCycle,$tplRMB);
        }

        //添加备货SetDate
        $countryIds = SysICountry::getCountryIdsByAreaId($areaId);
        $pCycleForSetDate = StockingSetDate::getPCycle($countryIds,$sku->classify_id);
        StockingSetDate::addSetDate($areaId,$sku->classify_id,$pCycleForSetDate,$transport->sea_day ?? 0);
    }
}
